<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="p" uri="/lion-platform" %>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	<link rel="stylesheet" href="${webcontext}/res/style/S.css" />
	<script type="text/javascript" src="${webcontext }/res/js/jquery.min.js"></script>
	<script type="text/javascript" src="${webcontext }/res/js/S.js"></script>
	<script type="text/javascript">
		$(function(){
			var select = new S.Select({
				el:'#projects_select',
				ds:'${webcontext}/${pathvar.admin}/br/project/projects',
				clear:false,
				render:function(p){
					return p.name;
				},
				change:function(p){
					var id = '';
					if(p){id = p.id;}
					tbl.load('${webcontext}/${pathvar.admin}/br/requirment/list?projectId='+id);
					modelSelect.load('${webcontext}/${pathvar.admin}/br/project/modules/list?projectId='+id);
				}
			});
			
			select.select.defer(select,200,0);
			
			var modelSelect = new S.Select({
				el:'#project_model',
				render:function(m){
					return m.name;
				},
				change:function(m){
					$('#modelId').val(m.id);
				}
			});
			
			var statusTbl = {
					'1':'<span class="badge info">等　待</span>',
					'2':'<span class="badge primary">进行中</span>',
					'3':'<span class="badge success">已完成</span>',
					'4':'<span class="badge inverse">抛　弃</span>',
			};
			
			var tbl = new S.Table({
				el:'#require_tbl',
				pager:true,
				pageSize:30,
				multiselect:false,
				renders:{
					'status':function(r){
						return statusTbl[r] || '';
					}
				},
				menus:{
					'修改':function(tr){
						var idx = $(tr).index();
						var data =tbl.data[idx];
						if(data == null){return;}
						editRequirement(data);
					},
					'删除':function(tr){
						var idx = $(tr).index();
						var data =tbl.data[idx];
						if(data == null){return;}
						deleteRequirement([data]);
					}
				},
				dbclick:function(data){
					editRequirement(data);
				}
			});
			
			$('#btnquery').click(function(){
				tbl.reload();
			});
			
			$('#btn_requirement_add').click(function(){
				
				var ps = select.getSelectedItem();
				if(!ps){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var dialog = new S.Dialog({
					title:'增加需求',
					width:700,
					height:500,
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/requirment/edit?projectId='+ps.id,
					buttons:{
						'保存':function(){
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/requirment/save',
								data:S.fixConfig({projectId:ps.id},this.form.param()),
								success:function(){
									new S.Info({text:'保存成功',type:'success'}).show();
									dialog.remove();
									tbl.reload();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dialog.show();
			});

			function editRequirement(data){

				var dialog = new S.Dialog({
					title:'编辑需求',
					width:700,
					height:500,
					modal:true,
					url:'${webcontext}/${pathvar.admin}/br/requirment/edit?id='+data.id,
					buttons:{
						'保存':function(){
							if(!this.form.validate()){return;}
							S.request({
								url:'${webcontext}/${pathvar.admin}/br/requirment/update',
								data:S.fixConfig({projectId:data.projectId},this.form.param()),
								success:function(){
									new S.Info({text:'保存成功',type:'success'}).show();
									dialog.remove();
									tbl.reload();
								}
							});
						},
						'关闭':function(){
							this.remove();
						}
					}
				});
				dialog.show();
			}

			function deleteRequirement(items){
				S.confirm('警告','确认要删除选中的需求?',function(){
					var ids = [];
					S.each(items,function(i,item){
						ids.push(item.id);
					});
					S.request({
						url:'${webcontext}/${pathvar.admin}/br/requirment/delete',
						data:{ids:ids.join(',')},
						success:function(){
							new S.Info({text:'删除成功',type:'success'}).show();
							tbl.reload();
						}
					});
				},'warn');
			}

			$('#btn_requirement_edit').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length !=1){new S.Info({text:'请选择要编辑的需求',type:'warn'}).show();return;}
				editRequirement(items[0]);
			});

			$('#btn_requirement_delete').click(function(){
				
				var items = tbl.getCheckedRow();
				if(items.length ==0){new S.Info({text:'请选择要删除的需求',type:'warn'}).show();return;}
				
				deleteRequirement(items);
			});

			$('#btn_export_requirements').click(function(){
				
				var ps = select.getSelectedItem();
				if(!ps){new S.Info({text:'请选择项目',type:'warn'}).show();return;}
				
				var param = S.formData(tbl.$form);
				param.projectId=ps.id;
				
				
				S.download('${webcontext}/${pathvar.admin}/br/requirment/export',param);
			});
			
		});
	</script>	
</head>
<body>
	<div class="filled-box">
			<div class="toolbar">
				<span>　项目：<input class="long" type="text" id="projects_select"></span> 
				<span class="btn" id="btn_requirement_add"><i class="icons page_add"></i> 增加</span>
				<span class="btn" id="btn_requirement_edit"><i class="icons page_edit"></i> 修改</span>
				<p:priv code="BR.R.DEL">
				<span class="btn" id="btn_requirement_delete"><i class="icons page_delete"></i> 删除</span>
				</p:priv>
				<span class="btn" id="btn_export_requirements"><i class="icons saved_exports"></i> 导出</span>
			</div>
			<div class="content">
			<form name="frm001">
				<input type="hidden" name="modelId" id="modelId"> 
				<table width="100%">
					<tr>
						<td>模块:<input type="text" id="project_model">   
							状态：<select name="status">
								<option value="">全部</option>
								<option value="1">等待</option>
								<option value="2">进行中</option>
								<option value="3">已完成</option>
								<option value="4">抛弃</option>
							</select>
						
						  <span class="btn" id="btnquery"><i class="icons magnifier"></i></span></td>
					</tr>
				</table>
			</form>
			<table id="require_tbl" ds="${webcontext }/${pathvar.admin}/blank" height="600" form="frm001">
				<tr>
					<td width="50" seq></td>
					<td width="50" checkall></td>
					<td width="100" col="status" sort="status">状态</td>
					<td>标题</td>
					<td width="200">模块</td>
					<td width="150">创建者</td>
					<td width="150">创建时间</td>
				</tr>
				<tr>
					<td>@{seq}</td>
					<td><input type="checkbox" name="id" value="@{id }" /></td>
					<td>@{status}</td>
					<td>@{title}</td>
					<td>@{moduleName}</td>
					<td>@{creatorName}</td>
					<td>@{createTime}</td>
				</tr>
			</table>
		</div>
	</div>
</body>
</html>